package org.UserHouse.Dao.Imp;

import org.UserHouse.Dao.AddressDao;
import org.UserHouse.Utils.Dbutils;
import org.UserHouse.entity.Address;
import org.UserHouse.entity.Order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author bin
 * @date 2024-12-16
 */
public class AddressDaoImp implements AddressDao {


    @Override
    public List<Address> selectPageAddress(Address address, int page, int limit) {
        List<Address> list = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        Connection conn = Dbutils.getConnection();
        PreparedStatement pst = null;
        ResultSet rst = null;
        StringBuilder sql = new StringBuilder("select * from ( ");
        sql.append(" select  a_id, u_user.u_id,  a_receiver,  a_phone, a_address,  a_is_default,  a_date,row_number() over (order by a_date asc) as ");
        sql.append(" rownumber from u_address  left join u_user on u_user.u_id = u_address.u_id where 1=1 ");
        if (address != null && address.getAaddress() != null) {
            sql.append(" and a_address like ?");
            params.add("%" + address.getAaddress() + "%");
        }
        sql.append(") AS t2 WHERE rownumber BETWEEN ? AND ? ");

        int startPage = (page - 1) * limit + 1;
        params.add(startPage);
        int endPage = page * limit;
        params.add(endPage);

        try {
            pst = conn.prepareStatement(sql.toString());
            for (int i = 0; i < params.size(); i++) {
                pst.setObject(i + 1, params.get(i));
            }
            rst = pst.executeQuery();
            while (rst.next()) {
                Address a = new Address();
                int Aid = rst.getInt(1);
                int Uid = rst.getInt(2);
                String Areceiver = rst.getString(3);
                String Aphone = rst.getString(4);
                String Aaddress = rst.getString(5);
                int Aisdefult = rst.getInt(6);
                Date Adate = rst.getDate(7);

                a.setAid(Aid);
                a.setUid(Uid);
                a.setAreceiver(Areceiver);
                a.setAphone(Aphone);
                a.setAaddress(Aaddress);
                a.setAisdefult(Aisdefult);
                a.setAdate(Adate);

                list.add(a);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public Long selectPageCount(Address address) {
        long r = 0;
        List<Address> list = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder("select COUNT(a_id) from u_address where 1=1 ");
        if (address != null && address.getAaddress() != null) {
            sql.append("and a_address like ?");
            params.add("%" + address.getAaddress() + "%");
        }
        Connection conn = Dbutils.getConnection();
        PreparedStatement pst = null;
        ResultSet rst = null;
        try {
            pst = conn.prepareStatement(sql.toString());
            for (int i = 0; i < params.size(); i++) {
                pst.setObject(i + 1, params.get(i));
            }
            rst = pst.executeQuery();
            if (rst.next()) {
                r = rst.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return r;
    }

    @Override
    public int AddAddress(Address address) {
        return 0;
    }

    @Override
    public int UpdateAddress(Address address) {
        int n = 0;

        return n;
    }

    @Override
    public int DeleteAddress(int Aid) {
        int n = 0;
        String sql = "delete u_address  where a_id=?";
        Connection conn = Dbutils.getConnection();
        PreparedStatement pst = null;
        ResultSet rst = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1, Aid);
            n = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return n;
    }
}
